﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class api_MemberUser : PageBase
{
    MemberUser bll = new MemberUser();
    string baseName = "会员";
    protected void Page_Load(object sender, EventArgs e)
    {
        ResponseData rd = isLogin();
        if (rd.code != 1)
        {
            Json(rd);
            return;
        }

        string action = Request["action"];
        switch (action)
        {
            case "GetList"://查询数据 
                GetList();
                break;
            case "Insert"://新增一条 
                Insert();
                break;
            case "Update"://更新一条 
                Update();
                break;
            case "Delete"://删除一条 
                Delete();
                break;
            case "Login"://登录 
                Login();
                break;
            case "GetUserList"://用户列表 下拉框数据
                GetUserList();
                break;
            case "UpdatePersonalCenter"://个人信息更新 
                UpdatePersonalCenter();
                break;
            default:
                break;
        }
    }

    private void UpdatePersonalCenter()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            Hashtable userInfo = CurrentLoginInfo();
            if (h_p["loginNum"].ToString() == userInfo["loginNum"].ToString())
            {
                h_p["id"] = userInfo["id"];
                h_p.Remove("loginNum");
                int i = bll.UpdatePersonalCenter(h_p);
                rd.code = i > 0 ? 1 : 0;
                rd.data = null;
                rd.message = i > 0 ? "更新个人信息成功！" : "更新个人信息失败!";
            }
            else
            {
                rd.code = 0;
                rd.message = "修改失败";
            }
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    private void GetUserList()
    {
        ResponseData rd = new ResponseData();
        try
        {
            DataTable dt = bll.GetUserList();
            int i = dt.Rows.Count;
            rd.code = i > 0 ? 1 : 0;
            rd.data = i > 0 ? dt : null;
            rd.message = i > 0 ? "获取"+ baseName + "列表成功！" : "获取" + baseName + "列表失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    private void Delete()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            object id = h_p["id"];
            if (id == null || id.ToString() == "")
            {
                rd.code = 0;
                rd.message = "请传id";
                Json(rd);
                return;
            }
            int i = bll.Delete(id.ToString());
            rd.code = i > 0 ? 1 : 0;
            rd.message = i > 0 ? "删除" + baseName + "成功！" : "删除" + baseName + "失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    private void Login()
    {
        ResponseData rd = new ResponseData();
        try
        {
            //DateTime t1 = new DateTime(2023, 11, 8, 18, 10, 10);
            //DateTime t2 = DateTime.Now;
            //int compNum = DateTime.Compare(t1, t2);
            //if(compNum != 1)
            //{
            //    rd.code = -1;
            //    rd.message = "临时使用已到期！";
            //    Json(rd);
            //    return;
            //}
            Hashtable h_p = getPostParameter_json();
            object loginNum = h_p["loginNum"];
            object loginPwd = h_p["loginPwd"];
            if(loginNum != null && loginPwd != null)
            {
                Hashtable h = new Hashtable();
                h["loginNum"] = loginNum;
                h["loginPwd"] = loginPwd;
                Hashtable h_user = bll.GetModel(h);
                int i = h_user.Count;
                Hashtable returnH = new Hashtable();
                string token = "";
                if (i > 0)
                {
                    string userType = h_user["userType"].ToString();
                    if(userType != "666")
                    {
                        ScriptClass sc = new ScriptClass();
                        ScriptClass.MethodRes mr = sc.TestNum(h_user);
                        if (mr.code == -1)
                        {
                            Json(mr);
                            return;
                        }
                    }
                    //else
                    //{
                    //    rd.code = -1;
                    //    rd.message = "暂无权限";
                    //    Json(rd);
                    //    return;
                    //}
                    string currentTime = new TimeHelper().FormatTimeString(GetDateTimeString()).AddDays(1).ToString("yyyy-MM-dd HH:mm:ss");
                    token = DES.Encrypt(loginNum + "|" + h_user["onlyCode"].ToString() + "|" + currentTime);
                    returnH["token"] = token; returnH["userInfo"] = h_user;
                    gameList gl = new gameList();
                    ArrayList list = gl.GetList();
                    returnH["gameList"] = list;
                    //returnH["menuList"] = Pub.GetMenuList(userType);
                    h_user.Remove("id"); h_user.Remove("loginPwd");
                }
                rd.code = i > 0 ? 1 : 0;
                rd.message = i > 0 ? "登录成功！" : "账号或密码错误";
                rd.data = returnH;
            }
            else
            {
                rd.code = -1;
                rd.message = "账号或密码错误";
            }
            
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        
        Json(rd);
    }

    private void Insert()
    {
        ResponseData rd = new ResponseData();
        try
        {

            Hashtable h_p = getPostParameter_json();
            Hashtable h = new Hashtable();
            h["loginNum"] = h_p["loginNum"];
            Hashtable h_user = bll.GetModel(h);
            int userCount = h_user.Count;
            int i = 0;
            if (userCount == 0)
            {
                h_p["userType"] = "2"; //默认普通用户
                h_p["onlyCode"] = Guid.NewGuid().ToString("N"); //唯一编码
                h_p["lastLoginTime"] = ""; //上次登录时间
                h_p["updateTime"] = ""; //更新时间
                h_p["createTime"] = GetDateTimeString(); //创建时间
                i = bll.Insert(h_p);
            }
            rd.code = i > 0 ? 1 : 0;
            rd.message = i > 0 ? "添加用户成功！" : "添加用户失败! 请确认是否存在";


        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }
    private void Update()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            int i = bll.Update(h_p);
            rd.code = i > 0 ? 1 : 0;
            rd.message = i > 0 ? "更新用户成功！" : "更新用户失败!";
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
    }

    public void GetList()
    {
        ResponseData rd = new ResponseData();
        try
        {
            Hashtable h_p = getPostParameter_json();
            int pageSize = h_p["pageSize"] == null ? 10 : Convert.ToInt32(h_p["pageSize"]);//每页显示行
            int pageNo = h_p["pageNum"] == null ? 1 : Convert.ToInt32(h_p["pageNum"]);//页码，起始值 1
            int total = 0;
            #region 查询条件
            string where = string.Empty;//where条件
            
            object loginNum = h_p["loginNum"];
            Hashtable h_parameters = new Hashtable();
            if (loginNum != null && loginNum.ToString() != "")
            {
                h_parameters["loginNum"] = loginNum;
                where += string.Format(" and loginNum like '%" + loginNum + "%'");
            }
            if (loginNum != null && loginNum.ToString() != "")
            {
                h_parameters["loginNum"] = loginNum;
                where += string.Format(" and loginNum like '%" + loginNum + "%'");
            }
            where += string.Format(" and userType <> '666'");
            #endregion
            ArrayList list = bll.GetList(pageSize, pageNo, out total, where);
            Hashtable h_res = new Hashtable();
            h_res.Add("list", list);
            h_res.Add("total", total);//数据总数
            rd.code = 1;
            rd.message = "获取成功";
            rd.data = h_res;
        }
        catch (Exception ex)
        {
            rd.code = -1;
            rd.message = "出错啦！错误详情：" + ex.Message;
        }
        Json(rd);
        
    }

}